/**
 * 
 */
package com.burgess.rtd.constants;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author aburgess
 *
 */
public class Dates {
	private SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	
	public String format(long time, boolean offset) {
		Date date = new Date();
		date.setTime(time);
		if (offset) {
			return dateformat.format(offsetTimeZone(date));
		} else {
			return dateformat.format(date);
		}
	}
	
	public String format(Date date) {
		return dateformat.format(offsetTimeZone(date));
	}
	
	public String format(Date date, String format) {
		SimpleDateFormat customformat = new SimpleDateFormat(format);
		
		return customformat.format(offsetTimeZone(date));
	}
	
	public Date parse(String datestr) {
		Date date = new Date();
		try {
			 date = dateformat.parse(datestr);
		} catch (ParseException e) {
			return null;
		}
		
		return offsetTimeZone(date);
	}

	public String offset(String datestr, String format) {
		SimpleDateFormat customformat = new SimpleDateFormat(format);
		Date date = new Date();
		try {
			date = dateformat.parse(datestr);
		} catch (ParseException e) {
			return "";
		}
		
		return customformat.format(offsetTimeZone(date));
	}
	
	private Date offsetTimeZone(Date date) {
		long time = date.getTime();
		time = time + TimeZone.getDefault().getOffset(time);
		date.setTime(time);
		
		return date;
	}
}
